<?php

namespace App\Http\Controllers\Xml;

use App\OrderOriginal;
use App\SettlementReport;
use App\ShowtimeSchedule;
use DOMDocument;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Sabre\Xml\Service;

class DataReportController extends Controller
{
    //
    public function index(Request $request){

//        if (!$business_date = $request->business_date){
//            return error('营业日缺失');
//        }
//
//        $list = ShowtimeSchedule::where('showtime_schedules.business_date','=',$business_date)
//            ->join('sessions','sessions.showtime_schedule_code','=','showtime_schedules.code')
//            ->select('sessions.*')
//            ->get();
//        $data = [];
//        foreach ($list as $val){
//            $info = [];
//
//        }


        $str = '<TicketReport Id="ID_TicketReport" Status="Past"><Cinema Code="11030010" Status="Normal"><Session><BusinessDate>2017-05-21</BusinessDate><ScreenCode>0000000000000001</ScreenCode><FilmCode>001100032015</FilmCode><SessionCode>0617051900100723</SessionCode><SessionDatetime>2017-05-21T14:00:00</SessionDatetime><LocalSalesCount>0</LocalSalesCount><LocalRefundCount>0</LocalRefundCount><LocalRefund>0.00</LocalRefund>
<LocalSales>0.00</LocalSales><OnlineSalesCount>0</OnlineSalesCount><OnlineRefund>0.00</OnlineRefund><OnlineRefundCount>0</OnlineRefundCount><OnlineSales>0.00</OnlineSales><PastSaleCount>0</PastSaleCount><PastSales>0.00</PastSales></Session>
<Session><BusinessDate>2017-05-21</BusinessDate><ScreenCode>0000000000000002</ScreenCode><FilmCode>001100042015</FilmCode><SessionCode>0617051900200719</SessionCode><SessionDatetime>2017-05-21T15:00:00</SessionDatetime><LocalSalesCount>0</LocalSalesCount>
<LocalRefundCount>0</LocalRefundCount><LocalRefund>0.00</LocalRefund><LocalSales>0.00</LocalSales><OnlineSalesCount>0</OnlineSalesCount><OnlineRefund>0.00</OnlineRefund><OnlineRefundCount>0</OnlineRefundCount><OnlineSales>0.00</OnlineSales>
<PastSaleCount>0</PastSaleCount><PastSales>0.00</PastSales></Session><Session><BusinessDate>2017-05-21</BusinessDate><ScreenCode>0000000000000003</ScreenCode><FilmCode>005100052015</FilmCode>
<SessionCode>0617051900300717</SessionCode>
<SessionDatetime>2017-05-21T16:00:00</SessionDatetime>
<LocalSalesCount>0</LocalSalesCount>
<LocalRefundCount>0</LocalRefundCount>
<LocalRefund>0.00</LocalRefund>
<LocalSales>0.00</LocalSales>
<OnlineSalesCount>0</OnlineSalesCount>
<OnlineRefund>0.00</OnlineRefund>
<OnlineRefundCount>0</OnlineRefundCount>
<OnlineSales>0.00</OnlineSales>
<PastSaleCount>0</PastSaleCount>
<PastSales>0.00</PastSales>
</Session>
<Session>
<BusinessDate>2017-05-21</BusinessDate>
<ScreenCode>0000000000000004</ScreenCode>
<FilmCode>001100012015</FilmCode>
<SessionCode>0617051900400721</SessionCode>
<SessionDatetime>2017-05-21T17:00:00</SessionDatetime>
<LocalSalesCount>0</LocalSalesCount>
<LocalRefundCount>0</LocalRefundCount>
<LocalRefund>0.00</LocalRefund>
<LocalSales>0.00</LocalSales>
<OnlineSalesCount>0</OnlineSalesCount>
<OnlineRefund>0.00</OnlineRefund>
<OnlineRefundCount>0</OnlineRefundCount>
<OnlineSales>0.00</OnlineSales>
<PastSaleCount>0</PastSaleCount>
<PastSales>0.00</PastSales>
</Session>
<Session>
<BusinessDate>2017-05-21</BusinessDate>
<ScreenCode>0000000000000005</ScreenCode>
<FilmCode>005100022015</FilmCode>
<SessionCode>0617051900500725</SessionCode>
<SessionDatetime>2017-05-21T18:00:00</SessionDatetime>
<LocalSalesCount>0</LocalSalesCount>
<LocalRefundCount>0</LocalRefundCount>
<LocalRefund>0.00</LocalRefund>
<LocalSales>0.00</LocalSales>
<OnlineSalesCount>0</OnlineSalesCount>
<OnlineRefund>0.00</OnlineRefund>
<OnlineRefundCount>0</OnlineRefundCount>
<OnlineSales>0.00</OnlineSales>
<PastSaleCount>0</PastSaleCount>
<PastSales>0.00</PastSales>
</Session>
<Session>
<BusinessDate>2017-05-21</BusinessDate>
<ScreenCode>0000000000000001</ScreenCode>
<FilmCode>001100042015</FilmCode>
<SessionCode>0617051900100727</SessionCode>
<SessionDatetime>2017-05-21T20:00:00</SessionDatetime>
<LocalSalesCount>0</LocalSalesCount>
<LocalRefundCount>0</LocalRefundCount>
<LocalRefund>0.00</LocalRefund>
<LocalSales>0.00</LocalSales>
<OnlineSalesCount>0</OnlineSalesCount>
<OnlineRefund>0.00</OnlineRefund>
<OnlineRefundCount>0</OnlineRefundCount>
<OnlineSales>0.00</OnlineSales>
<PastSaleCount>0</PastSaleCount>
<PastSales>0.00</PastSales>
</Session>
</Cinema>
</TicketReport>
';
        $str = str_replace("\n","",$str);
        $doc = new DOMDocument();
        $doc->loadXML($str);
        return base64_encode(sha1($doc->C14N()));


    }

    public function xml_index(Request $request){
        $list = SettlementReport::limit(2)->get();

        foreach ($list as $val){

        }
        $service = new Service();
        $xml =  $service->write( 'Data', [
            'TicketReport'=>[
                'value'=>[[
                    'name'=>'Cinema',
                    'attributes'=>['Code' => '10000136','Status'=>'Normal'],
                    'value'=>[
                            [
                            'name'=>'Session',
                            'value'=>[
                                'OrderCode'=>'订单编号'
                            ]
                        ],
                    ],
                ],

            ],
            'attributes'=>['Status'=>'Normal','Id'=>'ID_TicketReport']
            ]
        ]);

        return $xml;
    }

    public function json_index(Request $request){

        $list = SettlementReport::where('business_date','=','2017-05-21')->get();
        $list = SettlementReport::limit(2)->get();
        $TicketReport = $Cinema = $Session = [];
        $Cinema['Code'] = '11030010';
        $Cinema['Status'] = 'Normal';
        foreach ($list as $val){
            $info = [];
            $info['BusinessDate'] = strstr($val->business_date,' ',true);
            $info['ScreenCode'] = $val->screen_code;
            $info['FilmCode'] = $val->film_code;
            $info['SessionCode'] = $val->session_code;
            $info['SessionDatetime'] = str_replace(' ','T',$val->session_datetime);
            $info['LocalSalesCount'] = $val->local_sales_count;
            $info['LocalSales'] = '0.00';
            $info['LocalRefundCount'] = $val->local_refund_count;
            $info['LocalRefund'] = '0.00';
            $info['OnlineSalesCount'] = $val->online_sales_count;
            $info['OnlineSales'] = '0.00';
            $info['OnlineRefundCount'] = $val->online_refund_count;
            $info['OnlineRefund'] = '0.00';
            $info['PastSaleCount'] = $val->past_sales_count;
            $info['PastSales'] = '0.00';
            $Cinema['Session'][] = $info;
        }
        $TicketReport['Status'] = 'Past';
        $TicketReport['Id'] = 'ID_TicketReport';
        $TicketReport['Cinema'][] = $Cinema;

        $data['TicketReport'] = $TicketReport;
        return $data;

    }
}
